//
//  AppDelegate.swift
//  SwiftStory
//
//  Created by ZxjMacBookPro on 2020/4/11.
//  Copyright © 2020 ZxjMacBookPro. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let screen = UIScreen.main.bounds
        window = UIWindow.init(frame: screen)
        window?.backgroundColor = UIColor.white
        
//        let VC = ViewController()
//        let nav = UINavigationController(rootViewController: VC)
//        self.window?.rootViewController = nav
        // 2.注册监听
        NotificationCenter.default.addObserver(self, selector: #selector(changeRootViewController), name: NSNotification.Name(rawValue: ZXJSwitchRootViewController), object: nil)
        
//        let VC = MainViewCtr()
//        let VC = WelcomeViewCtr()
//        let sb = UIStoryboard(name: "Newfeature", bundle: nil)
//        let VC = sb.instantiateInitialViewController()!
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = VC
        window?.makeKeyAndVisible()

        return true
    }
    
}

extension AppDelegate{
    ///切换控制器
    @objc func changeRootViewController(notice: Notification){
        if notice.object as! Bool
        {
            let VC = MainViewCtr()
            window?.rootViewController = VC
        }else
        {
            let VC = WelcomeViewCtr()
            window?.rootViewController = VC
        }
    }
    
    /// 用于返回默认界面
    private func defaultViewController() -> UIViewController
    {
        // 1.判断是否登录
        if UserAcount.isLogin()
        {
            // 2.判断是否有新版本
            //            isNewVersion() ? UIStoryboard(name: "Newfeature", bundle: nil).instantiateInitialViewController()! : UIStoryboard(name: "Welcome", bundle: nil).instantiateInitialViewController()!
            if isNewVersion()
            {
//                let sb = UIStoryboard(name: "Newfeature", bundle: nil)
//                sb.instantiateInitialViewController()!
                return R.storyboard.newfeature.instantiateInitialViewController()!
            }else
            {
                return WelcomeViewCtr()
            }
        }
        
        // 没有登录
        return MainViewCtr()
    }
    
    /// 判断是否有新版本
    private func isNewVersion() -> Bool
    {
        // 1.加载info.plist
        // 2.获取当前软件的版本号
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        // 3.获取以前的软件版本号?
        let defaults = UserDefaults.standard
        let sanboxVersion = (defaults.object(forKey: "xxoo") as? String) ?? "0.0"
        // 4.用当前的版本号和以前的版本号进行比较
        // 1.0  0.0
        if currentVersion.compare(sanboxVersion) == ComparisonResult.orderedDescending
        {
            // 如果当前的大于以前的, 有新版本
            NSLog("有新版本")
            // 如果有新版本, 就利用新版本的版本号更新本地的版本号
            
            defaults.set(currentVersion, forKey: "xxoo")
            defaults.synchronize() // iOS7以前需要写, iOS7以后不用写
            return true
        }
        NSLog("没有新版本")
        return false
    }
}


func NSLog<T>(message: T, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) {
    #if DEBUG
    print("\((fileName as NSString).pathComponents.last!).\(methodName)[\(lineNumber)]:\(message)")
    #endif
}

